//
//  ViewControllerUserSaveModel.m
//  TEXT
//
//  Created by 刘博 on 2020/12/19.
//  Copyright © 2020 刘博. All rights reserved.
//

#import "ViewControllerUserSaveModel.h"
#import "PersonModel.h"

@interface ViewControllerUserSaveModel ()

@property (nonatomic, strong) NSMutableArray *array ;

@property (nonatomic, strong) NSLock *lock;

@property (nonatomic, assign) NSInteger kkk;

@end

@implementation ViewControllerUserSaveModel

- (void)viewDidLoad {
    [super viewDidLoad];
    PersonModel *person = [[PersonModel alloc] init];
    person.name = @"你好你好";
    person.gender = @"nv";
    NSData * personData  = [NSKeyedArchiver archivedDataWithRootObject:person];
    [[NSUserDefaults standardUserDefaults] setObject:personData forKey:@"kkk"];
    NSData *tmpData = [[NSUserDefaults standardUserDefaults] objectForKey:@"kkk"];
    PersonModel *tmpPerson = [NSKeyedUnarchiver unarchiveObjectWithData:tmpData];
    NSLog(@"名字名字名字%@",tmpPerson.name);
    self.lock = [[NSLock alloc] init];
    [self test];
    [self test2];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
     //   printf(" 哈哈哈格式化个数\n%ld", self.array.count);
        printf("哈哈哈哈哈哈%ld", self.kkk);
        
    });
    // Do any additional setup after loading the view.
}

- (void)test {
//    dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//     //   [self.lock lock];
//    for (int i = 0; i < 100; i ++) {
//            [self.array addObject:@(i)];
//    }
//     //   [self.lock unlock];
//    });
    
    for (int i = 0; i < 20000; i ++) {
        dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            [self.lock lock];
            self.kkk ++;
            [self.lock unlock];
           // [self.array addObject:@(i)];
        });
    }
}

- (void)test2 {
//    dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//        [self.lock lock];
//    for (int i = 0; i < 100; i ++) {
//            [self.array addObject:@(i)];
//    }
//        [self.lock unlock];
//    });
    
    for (int i = 0; i < 20000; i ++) {
        dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
          ///  [self.array addObject:@(i)];
            self.kkk ++;
        });
    }
}

#pragma  mark - lazy load

- (NSMutableArray *)array
{
    if (!_array) {
        _array = [NSMutableArray array];
    }
    return _array;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
